#include "TelephoneInfo.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

// struct TelephoneInfo
// {
//     char *name;
//     char *phone;
// };
// typedef struct TelephoneInfo Telinfo;

Telinfo *CreateTelinfo()
{
    Telinfo *s = (Telinfo *)malloc(sizeof(Telinfo));
    if (s == NULL)
    {
        printf("Createelinfo malloc error!\n");
        exit(-1);
    }
    FILE *f = fopen("./telephone_book.txt", "a");
    if (f == NULL)
    {
        printf("打开文件失败\n");
        return NULL;
    }
    char name[15];
    printf("请输入用户姓名：\n");
    scanf("%s", name);
    fputs(name, f);
    fputc('\n', f);

    char phone[12];
    printf("请输入用户电话号码：\n");
    scanf("%s", phone);
    fputs(phone, f);
    fputc('\n', f);

    s->name = strdup(name); // 堆上的字符串
    s->phone = strdup(phone);

    // FILE *file = fopen("telephone_book.txt", "a");
    // if (file == NULL)
    // {
    //     printf("无法打开文件以保存数据！\n");
    //     return NULL;
    // }
    // fprintf(file, "%s %s\n", s->name, s->phone);
    fclose(f);

    getchar();
    getchar();

    return s;
}

// void fscan(Telinfo *s)
// {
//     FILE *file = fopen("telephone_book.txt", "r");
//     if (!file)
//     {
//         return;
//     }
//     s->name = malloc(10);
//     s->phone = malloc(20);
//     fscanf(file, "name=%s tele=%s\n", s->name, s->phone);

//     fclose(file);
// }

Telinfo *GetTelinfo(FILE* f)
{
    char p[15] = {0};
    Telinfo *s = (Telinfo *)malloc(sizeof(Telinfo));
    if (s == NULL)
    {
        printf("Createelinfo malloc error!\n");
        exit(-1);
    }
    char c;
    int i = 0;
    while(( c = fgetc(f) )!= EOF)
    {
        if(c == '\n')
        {
            p[i] = '\0';
            i = 0;
            break;
        }
        p[i++] = c;
    }
    s->name = strdup(p);
    while(( c = fgetc(f) )!= EOF)
    {
        if(c == '\n')
        {
            p[i] = '\0';
            i = 0;
            break;
        }
        p[i++] = c;
    }
    s->phone = strdup(p);
    //fclose(f);
    return s;
    
}

void SaveTelinfo(void *p)
{
    FILE* f = fopen("./通讯录.txt","a");
    if(f == NULL)
    {
        printf("文件打开失败\n");
        return ;
    }
    Telinfo *s = (Telinfo *)p;
    fputs(s->name,f);
    fputc('\n',f);
    fputs(s->phone,f);
    fputc('\n',f);
    fclose(f);
}

void PrintTelinfo(void *p)
{
    Telinfo *s = (Telinfo *)p;
    printf("姓名：%s 电话号码：%s\n", s->name, s->phone);
}

void ClearTelinfo(void *p)
{
    Telinfo *s = (Telinfo *)p;
    free(s->name);
    free(s->phone);
    free(s);
}

bool IsEqual(void *p1, void *p2)
{
    Telinfo *s = (Telinfo *)p1;
    const char *value = (const char *)p2;
    return strcmp(s->name, value) == 0 || strcmp(s->phone, value) == 0;
}

int FindEqual(void *p1, void *p2)
{
    Telinfo *s = (Telinfo *)p1;
    const char *value = (const char *)p2;
    // return strcmp(s->name, value) || strcmp(s->phone, value);
    return strcmp(s->name, value);
}

bool FindEqual2(void *p1, void *p2)
{
    Telinfo *s1 = (Telinfo *)p1;
    Telinfo *s2 = (Telinfo *)p2;
    // const char *value = (const char *)p2;
    // return strcmp(s->name, value) || strcmp(s->phone, value);
    return strcmp(s1->name, s2->name) == 0;
}

int BiggerName(void *p1, void *p2)
{
    Telinfo *s1 = (Telinfo *)p1;
    Telinfo *s2 = (Telinfo *)p2;
    return strcmp(s1->name, s2->name);
}

bool FuzzyEqual(void *p1, void *p2)
{
    Telinfo *s1 = (Telinfo *)p1;
    const char *value = (const char *)p2;
    return strstr(s1->name, value) != NULL;
}
